package com.sskj.givecoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.FabiPublishListBean;
import com.sskj.givecoin.m.bean.HaiPageBean;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.m.http.JsonConverter;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.FabuRecordFragment;

import java.util.List;

import io.reactivex.Flowable;


public class FabuRecordPresenter extends BasePresenter<FabuRecordFragment> {
    public Flowable<List<FabiPublishListBean>> getData(int page, boolean isBuy, String startTime, String endTime) {
        return OkGo.<HttpData<HaiPageBean<FabiPublishListBean>>>get(HttpConfig.BASE_URL + HttpConfig.ORDER_LIST)
                .params("app", "app")
                .params("action", "brokerInfo")
                .params("type", isBuy ? "pmma" : "sell")
                .params("p", page)
                .params("size", 15)
                .params("b_time", startTime)
                .params("e_time", endTime)
                .converter(new JsonConverter<HttpData<HaiPageBean<FabiPublishListBean>>>() {})
                .adapt(new FlowableBody<>())
                .map(haiPageBeanHttpData -> haiPageBeanHttpData.getData().getRes());
    }

    public void cancelOrder(boolean isBuy, String id) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.FABI_PUBLISH_ORDER_CANCLE)
                .params("id", id)
                .params("type", isBuy ? "pmma" : "sell")
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            mView.delete();
                        }
                    }
                });
    }
}
